<template>
  <div class="user-info-container">
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div 
        class="nav-item" 
        :class="{ active: currentSection === 'profile' }"
        @click="currentSection = 'profile'"
      >个人资料</div>
      <div 
        class="nav-item" 
        :class="{ active: currentSection === 'security' }"
        @click="currentSection = 'security'"
      >安全中心</div>
      <div 
        class="nav-item" 
        :class="{ active: currentSection === 'notification' }"
        @click="currentSection = 'notification'"
      >通知设置</div>
      <div 
        class="nav-item" 
        :class="{ active: currentSection === 'privacy' }"
        @click="currentSection = 'privacy'"
      >隐私设置</div>
    </div>

    <!-- 内容区域 -->
    <component 
      :is="currentComponent" 
      v-if="currentSection !== 'profile'"
    />
    <template v-else>
      <!-- 原有的个人资料内容 -->
      <div class="profile-header">
        <div class="avatar-section">
          <div class="avatar-wrapper">
            <el-avatar 
              :size="140" 
              :src="userInfo.profilePicture"
              class="user-avatar"
            />
            <div class="avatar-upload">
              <el-upload
                class="avatar-uploader"
                :show-file-list="false"
                :before-upload="beforeAvatarUpload"
                :http-request="handleAvatarSuccess"
              >
                <el-button type="default" size="small" class="upload-btn">
                  <el-icon><Camera /></el-icon>
                  更换头像
                </el-button>
              </el-upload>
            </div>
          </div>
        </div>
        <div class="user-basic-info">
          <h1 class="username">{{ userInfo.userName }}</h1>
          <div class="user-meta">
            <span class="user-id">UID: {{ userInfo.userId }}</span>
          </div>
        </div>
      </div>

      <!-- 主要内容区域 -->
      <div class="profile-content">
        <!-- 个人信息 -->
        <div class="info-section">
          <h2 class="section-title">个人信息</h2>
          <div class="info-list">
            <div class="info-item">
              <div class="info-label">昵称</div>
              <div class="info-value">{{ userInfo.userName }}</div>
            </div>
            <div class="info-item">
              <div class="info-label">UID</div>
              <div class="info-value">{{ userInfo.userId }}</div>
            </div>
          </div>
        </div>

        <!-- 验证信息 -->
        <div class="info-section">
          <h2 class="section-title">验证信息</h2>
          <div class="info-list">
            <div class="info-item">
              <div class="info-label">身份认证</div>
              <div class="info-value">未认证</div>
            </div>
            <div class="info-item">
              <div class="info-label">国家/地区</div>
              <div class="info-value">中国</div>
            </div>
          </div>
        </div>

        <!-- 账户详情 -->
        <div class="info-section">
          <h2 class="section-title">账户详情</h2>
          <div class="info-list">
            <div class="info-item">
              <div class="info-label">邮箱</div>
              <div class="info-value">{{ maskEmail(userInfo.email) }}</div>
            </div>
            <div class="info-item">
              <div class="info-label">手机</div>
              <div class="info-value">--</div>
            </div>
          </div>
        </div>

        <!-- 关联账号 -->
        <div class="info-section">
          <h2 class="section-title">已关联的账号</h2>
          <div class="info-list">
            <div class="info-item">
              <div class="info-label">关联账号</div>
              <div class="info-value">--</div>
            </div>
            <div class="info-item">
              <div class="info-label">手续费等级</div>
              <div class="info-value">Level 1</div>
            </div>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { Camera } from '@element-plus/icons-vue'
import userInfoStore from '@/stores/userInfo'
import {usersUploadService} from "@/api/user/userupload"
import SecurityCenter from './SecurityCenter.vue'
import toast from "@/utils/toast";

const userInfo = ref({})
const currentSection = ref('profile')

const currentComponent = computed(() => {
  switch (currentSection.value) {
    case 'security':
      return SecurityCenter
    case 'notification':
      return null // TODO: Add notification settings component
    case 'privacy':
      return null // TODO: Add privacy settings component
    default:
      return null
  }
})

onMounted(() => {
  userInfo.value = userInfoStore().info
})

// 邮箱脱敏
const maskEmail = (email) => {
  if (!email) return ''
  const [name, domain] = email.split('@')
  const maskedName = name.slice(0, 3) + '***'
  return `${maskedName}@${domain}`
}

// 头像上传前的验证
const beforeAvatarUpload = (file) => {
  const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isJPG) {
    toast.error('头像只能是 JPG 或 PNG 格式!')
    return false
  }
  if (!isLt2M) {
    toast.error('头像大小不能超过 2MB!')
    return false
  }
  return true
}

// 头像上传成功的回调
const handleAvatarSuccess = async (options) => {
  try {
    const formData = new FormData()
    formData.append('file', options.file)
    
    const { code, data } = await usersUploadService(formData)

    if (code === 200) {
      userInfo.value.profilePicture = data
      userInfoStore().setInfo(userInfo.value)
      toast.success('头像更新成功')
      window.location.reload();
    } else {
      toast.error('头像上传失败')
    }
  } catch (error) {
    console.error('头像上传失败:', error)
    toast.error('头像上传失败，请稍后重试')
  }
}
</script>

<style scoped>
.user-info-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px;
}

.nav-menu {
  display: flex;
  gap: 40px;
  margin-bottom: 40px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e0e0e0;
}

.nav-item {
  font-size: 18px;
  color: #666;
  cursor: pointer;
  padding: 5px 0;
  position: relative;
  transition: color 0.3s ease;
  font-weight: 500;
}

.nav-item:hover {
  color: #333;
}

.nav-item.active {
  color: #333;
  font-weight: 600;
}

.nav-item.active::after {
  content: '';
  position: absolute;
  bottom: -16px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #333;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .nav-menu {
    gap: 20px;
    overflow-x: auto;
    padding-bottom: 10px;
    margin-bottom: 30px;
  }

  .nav-item {
    font-size: 14px;
    white-space: nowrap;
  }

  .nav-item.active::after {
    bottom: -11px;
  }
}

.profile-header {
  display: flex;
  gap: 40px;
  margin-bottom: 50px;
  padding-bottom: 30px;
  border-bottom: 1px solid #e0e0e0;
}

.avatar-section {
  flex-shrink: 0;
}

.avatar-wrapper {
  position: relative;
}

.user-avatar {
  border: 2px solid #e0e0e0;
  transition: transform 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.02);
}

.avatar-upload {
  margin-top: 15px;
  text-align: center;
}

.upload-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 8px 16px;
  border-radius: 4px;
  border: 1px solid #e0e0e0;
  background: #fff;
  transition: all 0.3s ease;
}

.upload-btn:hover {
  background: #f5f5f5;
}

.user-basic-info {
  padding-top: 10px;
}

.username {
  font-size: 28px;
  font-weight: 500;
  color: #333;
  margin: 0 0 15px 0;
}

.user-meta {
  color: #666;
  font-size: 14px;
}

.profile-content {
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.section-title {
  font-size: 20px;
  font-weight: 900;
  color: black;
  margin: 0 0 20px 0;
  padding-bottom: 10px;
  border-bottom: 1px solid #e0e0e0;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
}

.info-label {
  width: 120px;
  color: #666;
  font-size: 14px;
}

.info-value {
  flex: 1;
  color: #333;
  font-size: 14px;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .profile-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 20px;
  }

  .user-meta {
    justify-content: center;
  }

  .info-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .info-label {
    width: auto;
  }

  .username {
    font-size: 24px;
  }
}
.nav-menu {
  display: flex;
  flex-wrap: wrap;
  overflow: hidden;
  white-space: normal;
}


</style>